//
//  SAProductDeleteColorView.m
//  yidiantong
//
//  Created by 黄志刚 on 2020/10/13.
//

#import "SAProductDeleteColorView.h"
@interface SAProductDeleteColorView()

@property ( nonatomic , strong ) UILabel *hitLabel;
@property ( nonatomic , weak ) UIView *backGroundView;

@end

@implementation SAProductDeleteColorView

+(instancetype)shareCornView
{
    SAProductDeleteColorView * shareView = [[SAProductDeleteColorView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    return shareView;
}

-(instancetype)initWithFrame:(CGRect)frame
{
    if(self = [super initWithFrame:frame])
    {
        self.userInteractionEnabled = YES;
        self.backgroundColor = kColor(0, 0, 0, 0.6);
        [self creatUI];
    }
    return self;
    
}

-(void)creatUI
{
    
    
    UIView *backGroundView = [[UIView alloc] initWithFrame:CGRectMake(kScaleNum(50),(kScreenHeight-kScaleNum(160))/2.0, kScaleNum(275), kScaleNum(160))];
    backGroundView.backgroundColor = [UIColor whiteColor];
    backGroundView.layer.cornerRadius = kScaleNum(10);
    backGroundView.layer.masksToBounds = YES;
    backGroundView.userInteractionEnabled = YES;
    [self addSubview:backGroundView];
    self.backGroundView = backGroundView;
    
    
    UILabel *hitLabel1 = [[UILabel alloc] init];
       hitLabel1.textAlignment = NSTextAlignmentCenter;
       hitLabel1.textColor = kColor(51, 51, 51, 1);
       hitLabel1.font = kFont(16);
    hitLabel1.text = @"删除";
       [backGroundView addSubview:hitLabel1];
    
       [hitLabel1 mas_makeConstraints:^(MASConstraintMaker *make) {
           make.centerX.offset(kScaleNum(0));
           make.top.equalTo(@(kScaleNum(20)));
           make.height.mas_equalTo(kScaleNum(15));
       }];
    
    
    UILabel *hitLabel = [[UILabel alloc] init];
    hitLabel.textAlignment = NSTextAlignmentCenter;
    hitLabel.numberOfLines = 0;
    hitLabel.textColor = kColor(51, 51, 51, 1);
    hitLabel.font = kFont(13);
    [backGroundView addSubview:hitLabel];
    self.hitLabel = hitLabel;
    [hitLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(kScaleNum(20)));
        make.right.equalTo(@(kScaleNum(-20)));
        make.top.equalTo(@(kScaleNum(60)));
    }];
    
    UIButton *deleteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    deleteBtn.frame = CGRectMake(kScaleNum(0), kScaleNum(115), kScaleNum(137), kScaleNum(45));
    [deleteBtn setTitle:@"取消" forState:UIControlStateNormal];
    [deleteBtn setTitleColor:kColor(51, 51, 51, 1) forState:UIControlStateNormal];
    deleteBtn.titleLabel.font = kFont(13);
    [deleteBtn addTarget:self action:@selector(shareDissTapEvent) forControlEvents:UIControlEventTouchUpInside];
    [backGroundView addSubview:deleteBtn];
    
    UIButton *confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom];
       confirmBtn.frame = CGRectMake(kScaleNum(138), kScaleNum(115), kScaleNum(137), kScaleNum(45));
       [confirmBtn setTitle:@"确定" forState:UIControlStateNormal];
    confirmBtn.titleLabel.font = kFont(13);
          [confirmBtn setTitleColor:kRedColor forState:UIControlStateNormal];
       [confirmBtn addTarget:self action:@selector(confirmationTapEvent) forControlEvents:UIControlEventTouchUpInside];
       [backGroundView addSubview:confirmBtn];
    
    UIView *cutView = [[UIView alloc] initWithFrame:CGRectMake(0, kScaleNum(115), kScaleNum(275), kScaleNum(1))];
    cutView.backgroundColor = kColor(230, 230, 230, 1);
    [backGroundView addSubview:cutView];
    
    UIView *cutView1 = [[UIView alloc] initWithFrame:CGRectMake(kScaleNum(137), kScaleNum(115), kScaleNum(1), kScaleNum(45))];
    cutView1.backgroundColor = kColor(230, 230, 230, 1);
    [backGroundView addSubview:cutView1];
}

-(void)setString:(NSString *)str
{
    self.hitLabel.text = [NSString stringWithFormat:@"是否删除自定义中的“%@”？",str];
}

- (void)showView:(UIViewController *)Sview
{
    if(Sview == nil){
        
        [[[UIApplication sharedApplication].windows lastObject] addSubview:self];
        
    }else{
        //[view addSubview:self];
        [Sview.view addSubview:self];
    }
    
}

-(void)shareDissTapEvent
{
    
    [UIView animateWithDuration:0.5f animations:^{
        
        self.alpha = 0.0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

-(void)confirmationTapEvent
{
    if (self.deleteColorBlock)
    {
        self.deleteColorBlock();
    }
    
    [UIView animateWithDuration:0.5f animations:^{

        self.alpha = 0.0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
 
    CGPoint point = [[touches anyObject] locationInView:self];
    point = [self.backGroundView.layer convertPoint:point fromLayer:self.layer];
    if (![self.backGroundView.layer containsPoint:point]) {
       [UIView animateWithDuration:0.5f animations:^{

               self.alpha = 0.0;
           } completion:^(BOOL finished) {
               [self removeFromSuperview];
           }];
    
    }
 

}

@end

